package bswd;

//download HtmlUtil at http://sourceforge.net/projects/htmlunit/files/
//and add all the jar files in the folder to your build path

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HttpUnitTest {
	public static void main(String[] args) {
		try{
		    final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
		    client.setJavaScriptEnabled(false);
		    System.out.println("Start connecting...");
            final HtmlPage page = client.getPage("http://jw.nju.edu.cn/root/index.html");
            //get all the table elements
            System.out.println("Successfully connected, start processing...");
            List<HtmlElement> list = page.getElementsByTagName("table");
            //get the one with the list of news
            HtmlElement what_we_want = list.get(8);
            //get all the td elements
            list = what_we_want.getElementsByTagName("td");
            //get all the a elements in <td>s
            ArrayList<HtmlElement> titleList = new ArrayList<HtmlElement>();
            ArrayList<HtmlElement> pubTimeList = new ArrayList<HtmlElement>();
            List<HtmlElement> tempList;
            for(HtmlElement item : list){
                tempList = item.getElementsByTagName("a");
                if(!tempList.isEmpty()){
                    titleList.addAll(tempList);
                }
                
                tempList = item.getElementsByTagName("font");
                if(!tempList.isEmpty()){
                    pubTimeList.addAll(tempList);
                }
            }
            System.out.println((titleList.size() == pubTimeList.size()));
            System.out.println("Start writing file...");
            File f = new File("D:\\test.txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(f));
            int listLength = titleList.size();
            for(int i = 0;i < listLength;i++){
                writer.write(titleList.get(i).getTextContent());
                writer.write("    " + pubTimeList.get(i).getTextContent());
                writer.write("\r\n");
            }
            System.out.println("All done...");
            writer.close();
        }catch(Exception e){
            //do nothing
        }
    }

}
